package com.occludens.admin.ui;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.googlecode.objectify.ObjectifyService;
import com.googlecode.objectify.Ref;
import com.googlecode.objectify.annotation.Cache;
import com.googlecode.objectify.annotation.Index;
import com.googlecode.objectify.annotation.Load;
import com.googlecode.objectify.annotation.Subclass;

@Subclass(index=true)
@Cache
public class Channel extends AdminObjectUI {
	@Index private String link;
	@Load private List<Ref<Command>> commands = new ArrayList<Ref<Command>>();
	
	protected Channel() {} // Mandatory for Objectify
	
	public Channel(String name) {
		super(name);
	}
	
	public Channel getChannel(String name) {
		return ObjectifyService.ofy().load().type(Channel.class).filter("name", name).first().now();
	}
	
	public String getLink() {
		return this.link;
	}
	
	public void setLink(String link) {
		this.link = link;
	}
	
	public List<Command> getCommands() {
		List<Command> returnList = new ArrayList<Command>();
		Iterator<Ref<Command>> commandsItr = this.commands.iterator();
		while (commandsItr.hasNext()) {
			returnList.add(commandsItr.next().get());
		}//End of while commandsItr
		return returnList;
	}

	public void setCommands(List<Command> commands) {
		Iterator<Command> commandsItr = commands.iterator();
		while (commandsItr.hasNext()) {
			Ref<Command> commandRef = Ref.create(commandsItr.next());
			if (!this.commands.contains(commandRef)) {
				this.commands.add(commandRef);
			}
		}//End of while commandsItr
	}

	public void addCommands(Command... commands) {
		for (int i=0; i<commands.length; i++) {
			Ref<Command> commandRef = Ref.create(commands[i]);
			if (!this.commands.contains(commandRef)) {
				this.commands.add(commandRef);
			}
		}
	}

	public void removeCommands(Command... commands) {
		for (int i=0; i<commands.length; i++) {
			Ref<Command> commandRef = Ref.create(commands[i]);
			if (this.commands.contains(commandRef)) {
				this.commands.remove(commandRef);
			}
		}
	}
}
